<?php
class IndexBusi {
	
	public function __construct(){
		tsLoad('/webyufei/busi/CommonBusi.class.php');
	}
	
	public function getFloorTreeDataProxy() {
		$floorTypeList = $this->getFloorType();
		$floorData = $this->getFloorData();
		$floorProcutType = $this->getFloorProductType();
		$productType = $this->getProductTypeData();
		$floorTypeList = CommonBusi::OneToMany($floorTypeList, $floorData, 'viewDataFloorId', 'floorDataChild');
		$floorTypeList = CommonBusi::ManyToMany($floorTypeList, $floorProcutType, $productType, 'viewDataFloorId', 'prodTypeId', 'floorProductChild');
		$floorTypeList = CommonBusi::getTree($floorTypeList, 0, 'viewDataFloorId', 'viewDataFloorParentId', 'child');
		return $floorTypeList;
	}
	
	public function getSimpleTreeDataProxy() {
		$simpleData = $this->getSimpleData();
		$dataType = $this->getDataType();
		$dataTypeList = CommonBusi::OneToMany($dataType, $simpleData, 'viewSimpleDataTypeId', 'simpleDataChild');
		$dataTypeList = CommonBusi::getTree($dataTypeList, 0, 'viewSimpleDataTypeId', 'viewSimpleDataTypeParentId', 'child');
		return $dataTypeList;
	}
	
	/*
	 * 取得楼层分类
	 */
	public function getFloorType() {
		global $mysqlTools;
		$sql = "
            SELECT 
				`viewDataFloorId`,
			    `viewDataFloorParentId`,
			    `viewDataFloorName`,
			    `viewDataFloorDesc`,
			    `viewDataFloorOrder`,
			    `viewDataFloorIsUsed`,
			    `viewDataFloorLevel`,
			    `viewDataFloorColor`
			FROM `view_data_floor`
            WHERE `viewDataFloorIsUsed` = 1
			ORDER BY `viewDataFloorOrder` ASC
				
        ";
		$list = $mysqlTools->selectSql ( $sql );
		return $list;
	}
	
	/*
	 * 取得楼层数据
	*/
	public function getFloorProductType() {
		global $mysqlTools;
		$sql = "
            SELECT 
				`viewDataFloorId`,
			    `prodTypeId`,
			    `viewDataCateName`
			FROM `view_data_floor_product_type`
        ";
		$list = $mysqlTools->selectSql ( $sql );
		return $list;
	}
	
	/*
	 * 取得产品类型
	*/
	public function getProductTypeData() {
		global $mysqlTools;
		$sql = "
            SELECT 
				`prodTypeId`,
			    `prodTypeName`,
			    `prodTypeDesc`,
			    `prodTypeOrder`,
			    `prodTypeIsHot`,
			    `prodTypeCateIndex`
			FROM `product_type`
			WHERE `prodTypeState` = 1
			ORDER BY `prodTypeOrder` ASC
        ";
		$list = $mysqlTools->selectSql ( $sql );
		return $list;
	}
	
	/*
	 * 取得楼层数据
	*/
	public function getFloorData() {
		global $mysqlTools;
		$sql = "
            SELECT
				`d`.`dataFloorDetailId`,
				`d`.`viewDataFloorId`,
				`d`.`dataFloorDetailName`,
				`d`.`dataFloorDetailDesc`,
				`d`.`dataFloorDetailOrder`,
				`d`.`photoId`,
				`d`.`dataFloorDetailUrl`,
				`d`.`prodId`,
				`i`.`photoUrl`,
				`i`.`photoWidth`,
				`i`.`photoHeight`,
				`i`.`photoSize`
			FROM
			`view_data_floor_detail` `d`
			LEFT JOIN
			`sys_photo` `i` ON `d`.`photoId` = `i`.`photoId`
			ORDER BY `d`.`dataFloorDetailOrder` ASC
        ";
		$list = $mysqlTools->selectSql ( $sql );
		return $list;
	}
	
	/*
	 * 取得楼层数据
	*/
	public function getSimpleData() {
		global $mysqlTools;
		$sql = "
            SELECT 
				`d`.`viewSimpleDataId`,
			    `d`.`viewSimpleDataTypeId`,
			    `d`.`viewSimpleDataName`,
			    `d`.`viewSimpleDataDesc`,
			    `d`.`viewSimpleDataOrder`,
				`d`.`viewSimpleDataColor`,
			    `d`.`viewSimpleDataCreateTime`,
			    `d`.`photoId`,
			    `d`.`viewSimpleDataUrl`,
			    `d`.`prodId`,
				`i`.`photoUrl`,
				`i`.`photoWidth`,
				`i`.`photoHeight`,
				`i`.`photoSize`
			FROM `view_simple_data` `d`
			LEFT JOIN
				`sys_photo` `i` ON `d`.`photoId` = `i`.`photoId`
			ORDER BY `d`.`viewSimpleDataOrder` ASC
        ";
		$list = $mysqlTools->selectSql ( $sql );
		return $list;
	}
	/*
	 * 取得楼层分类
	*/
	public function getDataType() {
		global $mysqlTools;
		$sql = "
			SELECT 
				`viewSimpleDataTypeId`,
			    `viewSimpleDataTypeParentId`,
			    `viewSimpleDataTypeName`,
			    `viewSimpleDataTypeDesc`,
			    `viewSimpleDataTypeOrder`,
			    `viewSimpleDataTypeIsUsed`
			FROM `view_simple_data_type`
			WHERE `viewSimpleDataTypeIsUsed` = 1
			ORDER BY `viewSimpleDataTypeOrder` ASC
        ";
		$list = $mysqlTools->selectSql ( $sql );
		return $list;
	}
	
	
	/*
	 * 取得热门行业
	*/
	public function getHotProductType() {
		global $mysqlTools;
		$productTypeTree = $mysqlTools->selectSql ( "
    			SELECT 
					`t`.`prodTypeId`,
				    `t`.`prodTypeName`,
				    `t`.`prodTypeDesc`,
				    `t`.`prodTypeCategory`,
				    `t`.`prodTypeIndex`,
				    `t`.`prodTypeOrder`,
				    `t`.`prodTypeParentId`,
				    `t`.`prodTypeCreateTime`,
				    `t`.`prodTypeState`,
				    `t`.`prodTypeQuantity`,
				    `t`.`prodTypeIndexCode`,
				    `t`.`prodTypeIsHot`,
				    `t`.`prodTypeCateIndex`,
					`i`.`photoId`,
					`i`.`photoUrl`,
					`i`.`photoWidth`,
					`i`.`photoHeight`,
					`i`.`photoSize`
				FROM `product_type` `t`
				LEFT JOIN
			    	`sys_photo` `i` ON `t`.`prodTypePhotoId` = `i`.`photoId` 
    			WHERE
					`t`.`prodTypeState` = 1
				AND `t`.`prodTypeIsHot` = 1 
    			ORDER BY `t`.`prodTypeOrder` ASC
    	" );
		$productTypeTree = CommonBusi::getTree($productTypeTree, 0, 'prodTypeId', 'prodTypeParentId', 'child');
		return $productTypeTree;
	}
	/**
	 * 取得树形数据顶级父类
	 *
	 * @return Array
	 */
	public function getRootParent($items,$idClumn,$pidClumn) {
		$result = array();
		foreach ($items as $value) {
			$isRoot = true;
			foreach ($items as $list) {
				if($value[$pidClumn]==$list[$idClumn]){
					$isRoot = false;
					continue;
				}
			}
			if($isRoot){
				$result[] = $value;
			}
		}
		return $result;
	}
	
	/**
	 * 增加产品次数
	 */
	public function addVisitCount($condition) {
		global $mysqlTools;
		// 参数处理注入SQL安全问题
		if(empty($condition['url']))
			return false;
		$url = rtrim($condition['url'], "/");
		try {
			$sql = "UPDATE `product` SET `visitCount` = `visitCount`+1";
			$sql .= " WHERE `prodUrl` = '$condition[url]' OR `prodUrl` = '$url' OR `shopUrl` = '$condition[url]' OR `shopUrl` = '$url'" ;
			$mysqlTools->executeSql($sql);
			$sql = "UPDATE `view_simple_data` SET `visitCount` = `visitCount`+1";
			$sql .= " WHERE `viewSimpleDataUrl` = '$condition[url]' OR `viewSimpleDataUrl` = '$url'";
			$mysqlTools->executeSql($sql);
			$sql = "UPDATE `view_data_floor_detail` SET `visitCount` = `visitCount`+1";
			$sql .= " WHERE `dataFloorDetailUrl` = '$condition[url]' OR `dataFloorDetailUrl` = '$url'";
			$mysqlTools->executeSql($sql);
		} catch ( \Exception $e ) {
			return false;
		}
		return true;
	}
}

?>